package com.huawei.petSystemdemo.ui.dashboard;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.hms.location.LocationCallback;
import com.huawei.hms.location.LocationResult;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.adapter.ShopAdapter;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.city.CityActivity;
import com.huawei.petSystemdemo.databinding.FragmentDashboardBinding;
import com.huawei.petSystemdemo.utils.LocationUtils;
import com.huawei.petSystemdemo.utils.MyRequest;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DashboardFragment extends Fragment {
    private RecyclerView shop_recycle;
    private int current = 1;
    private int size = 8;
    private Map<String, Object> requestBody = new HashMap<>();
    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;
    private double lon;
    private double lat;
    private TextView city_choose;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel = new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        //调用百度地图，经纬度转化为实际地址
        // 在OnCreate方法里创建地理编码检索实例
        //GeoCoder geoCoder = GeoCoder.newInstance();


        shop_recycle = root.findViewById(R.id.shop_petRecycle);


        new LocationUtils(getActivity(), "MainActivy", new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);

                Location location = locationResult.getLastLocation();
                lon = location.getLongitude();
                lat = location.getLatitude();

                System.out.println(location.getLongitude() + " : " + location.getLatitude());
            }
        }).locate();

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(RecyclerView.VERTICAL);

        shop_recycle.setLayoutManager(manager);

        requestBody.put("current", current + "");
        requestBody.put("size", size + "");
        requestBody.put("lon", lon + "");
        requestBody.put("lat", lat + "");
        List<Shop> shop_list = new ArrayList<>();

        MyRequest.get("/shop/getAll", requestBody, json -> {
            System.out.println(json);
            JSONObject obj = JSON.parseObject(json);
            // int statusCode = obj.getInteger("statusCode");
            //String errMessage = obj.getString("errMessage");
            List<Shop> data = obj.getJSONArray("data").toJavaList(Shop.class);

            System.out.println(data);
            shop_list.addAll(data);
            ShopAdapter adapter = new ShopAdapter(shop_list, getContext());
            shop_recycle.setAdapter(adapter);
        });


        //商店位置选择
        city_choose = root.findViewById(R.id.city_choose);
        city_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CityActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}